package com.example.shop.goods.dto;

import com.example.shop.common.bo.Page;
import com.example.shop.goods.enums.GoodsSortRules;
import com.example.shop.goods.enums.HighLightSetup;
import com.example.shop.goods.enums.SortOrder;
import com.example.shop.pojo.GoodsType;
import lombok.Data;
import lombok.Getter;
import org.jetbrains.annotations.Range;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

@Data
public class GoodsQueryParam {
    public static final String QUERY_SQL =
        " " +
        "<choose>" +
            "<when test = 'ew != null'>" +
                "<if test='ew.merchantId != null'>" +
                    "g.merchant_id = ${ew.merchantId} and " +
                "</if>" +
                "<if test='ew.goodsId != null'>" +
                    "g.goods_id = ${ew.goodsId} and " +
                "</if>" +
                "<if test='ew.goodsType != null'>" +
                    "g.goods_type = #{ew.goodsType} and " +
                "</if>" +
                "<if test='ew.goodsName != null'>" +
                    "g.goods_name = #{ew.goodsName} and " +
                "</if>" +
                "<if test='ew.fuzzyGoodsName != null'>" +
                    "g.goods_name LIKE '%${ew.fuzzyGoodsName}%' and " +
                "</if>" +
                "<if test='ew.shelfState != null'>" +
                    "g.shelf_state = #{ew.shelfState} and " +
                "</if>" +
                RangeQueryParam.RANGE_SQL +
                "ORDER BY " + GoodsSortRules.SORT_SQL+SortOrder.SORT_WAY_SQL+
                "<if test='ew.page != null'>" +
                    "limit ${(ew.page.pageIndex-1)*ew.page.pageSize},${ew.page.pageSize}"+
                "</if>"+
            "</when>" +
            "<otherwise>" +
                " 1 "+
            "</otherwise>" +
        "</choose>" +
        " ";
    //根据商家
    protected Long merchantId;
    //根据商品id进行查询
    protected Long goodsId;
    //根据商品的类型进行查询
    protected GoodsType goodsType;
    //根据商品名称进行查询
    protected String goodsName;
    //根据商品名称进行查询,模糊查询
    protected String fuzzyGoodsName;
    //根据商品的上架状态进行查询
    protected Integer shelfState;
    //范围查询参数
    @Valid
    protected List<RangeQueryParam> rangeQueryParams;
    //查询结果的排序规则
    protected GoodsSortRules sortRules;
    //排序顺序
    protected SortOrder sortOrder = SortOrder.ASC;
    //高亮设置
    private HighLightParam highLight;
    //分页
    protected Page page = new Page(1);



    private GoodsQueryParam(){
    }

    public static Builder builder(){
        return new Builder();
    }

    public static class Builder{
        protected GoodsQueryParam goodsQueryParam;

        public Builder(){
            goodsQueryParam = new GoodsQueryParam();
        }
        public GoodsQueryParam build(){
            return goodsQueryParam;
        }

        public Builder setMerchantId(Long merchantId) {
            this.goodsQueryParam.merchantId = merchantId;
            return this;
        }

        public Builder setGoodsId(Long goodsId) {
            goodsQueryParam.goodsId = goodsId;
            return this;
        }

        public Builder setGoodsType(GoodsType goodsType) {
            goodsQueryParam.goodsType = goodsType;
            return this;
        }
        public Builder setGoodsName(String goodsName) {
            goodsQueryParam.goodsName = goodsName;
            return this;
        }
        public Builder setFuzzyGoodsName(String fuzzyGoodsName) {
            goodsQueryParam.fuzzyGoodsName = fuzzyGoodsName;
            return this;
        }

        public Builder setShelfState(Integer shelfState) {
            goodsQueryParam.shelfState = shelfState;
            return this;
        }

        public Builder addRangeQueryParam(RangeQueryParam rangeQueryParam){
            if(goodsQueryParam.rangeQueryParams == null){
                goodsQueryParam.rangeQueryParams = new ArrayList<>();
            }
            goodsQueryParam.rangeQueryParams.add(rangeQueryParam);
            return this;
        }

        public Builder setGoodsSortRules(GoodsSortRules goodsSortRules) {
            goodsQueryParam.sortRules = goodsSortRules;
            return this;
        }
        public Builder setSortOrder(SortOrder sortOrder) {
            goodsQueryParam.sortOrder = sortOrder;
            return this;
        }

        public Builder setHighLight(HighLightParam highLight) {
            goodsQueryParam.highLight = highLight;
            return this;
        }

        public Builder setPage(Page page) {
            goodsQueryParam.page = page;
            return this;
        }

    }
}
